package com.lfg.leetcode.leetcode200;

import java.util.HashMap;
import java.util.Map;

public class No273numberToWords {

    static Map<Integer, String> data = new HashMap<>(64);

    static {
        data.put(0, "Zero");
        data.put(1, "One");
        data.put(2, "Two");
        data.put(3, "Three");
        data.put(4, "Four");
        data.put(5, "Five");
        data.put(6, "Six");
        data.put(7, "Seven");
        data.put(8, "Eight");
        data.put(9, "Nine");
        data.put(10, "Ten");
        data.put(11, "Eleven");
        data.put(12, "Twelve");
        data.put(13, "Thirteen");
        data.put(14, "Fourteen");
        data.put(15, "Fifteen");
        data.put(16, "Sixteen");
        data.put(17, "Seventeen");
        data.put(18, "Eighteen");
        data.put(19, "Nineteen");
        data.put(20, "Twenty");
        data.put(30, "Thirty");
        data.put(40, "Forty");
        data.put(50, "Fifty");
        data.put(60, "Sixty");
        data.put(70, "Seventy");
        data.put(80, "Eighty");
        data.put(90, "Ninety");
        data.put(50, "Fifty");
    }

    public static void main(String[] args) {
        String ret = numberToWords(1234567);
        System.out.println(ret);
    }

    public static String numberToWords(int num) {
        StringBuilder sb = new StringBuilder();
        if (num == 0) {
            sb.append(data.get(num));
            return sb.toString();
        }
        if (num >= 1000000000) {
            sb.append(toWords(num / 1000000000)).append(" Billion");
            num %= 1000000000;
        }

        if (num >= 1000000) {
            if (sb.length() > 0) {
                sb.append(" ");
            }
            sb.append(toWords(num / 1000000)).append(" Million");
            num %= 1000000;
        }
        if (num >= 1000) {
            if (sb.length() > 0) {
                sb.append(" ");
            }
            sb.append(toWords(num / 1000)).append(" Thousand");
            num %= 1000;
        }
        if (num > 0) {
            if (sb.length() > 0) {
                sb.append(" ");
            }
            sb.append(toWords(num));
        }
        return sb.toString().trim();
    }

    public static String toWords(int num) {
        StringBuilder sb = new StringBuilder();
        if (num == 0) {
            if (sb.length() > 0) {
                sb.append(" ");
            }
            sb.append(data.get(num));
            return sb.toString();
        }
        if (num >= 100) {
            int n = num / 100;
            if (sb.length() > 0) {
                sb.append(" ");
            }
            sb.append(data.get(n)).append(" Hundred");
            num %= 100;
        }
        if (num < 20 && num >= 10) {
            if (sb.length() > 0) {
                sb.append(" ");
            }
            sb.append(data.get(num));
            return sb.toString();
        }
        if (num >= 20) {
            int n = num / 10 * 10;
            if (sb.length() > 0) {
                sb.append(" ");
            }
            sb.append(data.get(n));
            num %= 10;
        }
        if (num > 0) {
            if (sb.length() > 0) {
                sb.append(" ");
            }
            sb.append(data.get(num));
        }
        return sb.toString();
    }


}
